package com.hpin.fansea.service;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.hpin.fansea.R;
import com.hpin.fansea.base.BaseActivity;
import com.hpin.fansea.bean.ExpressDetailBean;
import com.hpin.fansea.bean.ResultBaseBean;
import com.hpin.fansea.utils.Constant;
import com.hpin.fansea.utils.HttpUtil;
import com.lidroid.xutils.http.RequestParams;

/**
 *
 * 快递详情页面
 * Created by Aaron on 15/9/16.
 */
public class ExpressDetailActivity extends BaseActivity {

    private String expressId;
    private TextView tv_goods;
    private TextView tv_order_time;
    private TextView tv_receiver;
    private TextView tv_address;
    private TextView tv_status;
    private Button btn_sign;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_express_detail);

        expressId = getIntent().getStringExtra("expressId");

        initWidget();

        getExpressDetail();

    }

    private void initWidget() {

        ImageView iv_left = (ImageView) findViewById(R.id.iv_left);
        TextView tv_title = (TextView) findViewById(R.id.tv_title);

        tv_title.setText(getResources().getString(R.string.express));

        iv_left.setOnClickListener(this);

        tv_goods = (TextView) findViewById(R.id.tv_goods);
        tv_order_time = (TextView) findViewById(R.id.tv_order_time);
        tv_receiver = (TextView) findViewById(R.id.tv_receiver);
        tv_address = (TextView) findViewById(R.id.tv_address);
        tv_status = (TextView) findViewById(R.id.tv_status);

        btn_sign = (Button) findViewById(R.id.btn_sign);

        btn_sign.setOnClickListener(this);
    }

    /**
     * 获取快递详情
     */
    private void getExpressDetail() {

        HttpUtil.getInstance().setGetRequest("获取中...", this, Constant.WEB_ROOT + Constant.EXPRESSDETAIL +
                "?token=" + sp.getString(Constant.token, "") + "&expressId=" + expressId, new HttpUtil.SuccessListener() {
            @Override
            public void setOnSuccessListener(String str) {

                try {

                    ExpressDetailBean beanData = JSONObject.parseObject(str, ExpressDetailBean.class);

                    if (Constant.STATUS_SUCESS.equals(beanData.status)) {

                        //商品
                        tv_goods.setText(beanData.result.collectingGoods);
                        //预约时间
                        tv_order_time.setText(beanData.result.appointmentTime);
                        //收件人
                        tv_receiver.setText(beanData.result.addressee);
                        //地址
                        tv_address.setText(beanData.result.address);
                        //状态
                        if (Constant.EXPRESS_RECEOVERED.equals(beanData.result.status)) {

                            tv_status.setText("已签收");
                            btn_sign.setText("已签收");
                            btn_sign.setClickable(false);
                            btn_sign.setEnabled(false);
                            btn_sign.setFocusable(false);
                            btn_sign.setBackgroundColor(getResources().getColor(R.color.gray));

                        } else if (Constant.EXPRESS_NOT_RECEOVER.equals(beanData.result.status)) {

                            tv_status.setText("未签收");
                            btn_sign.setText("未签收");
                        }

                    } else {
                        showToast(beanData.message);
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        }, null);

    }

    @Override
    public void widgetClick(View v) {

        switch (v.getId()) {

            case R.id.iv_left:
                finish();
                break;

            case R.id.btn_sign://签收

                receiverExpress();

                break;

        }

    }

    /**
     * 签收快递
     */
    private void receiverExpress() {

        RequestParams params = new RequestParams();
        params.addBodyParameter("token", sp.getString(Constant.token,""));
        params.addBodyParameter("expressId", expressId);

        HttpUtil.getInstance().setPostRequest("签收中...", this, Constant.WEB_ROOT + Constant.RECEIVEREXPRESS, params, new HttpUtil.SuccessListener() {
            @Override
            public void setOnSuccessListener(String str) {

                try {

                    ResultBaseBean resultBaseBean = JSONObject.parseObject(str, ResultBaseBean.class);

                    if (Constant.STATUS_SUCESS.equals(resultBaseBean.status)) {

                        showToast("签收成功");

                        btn_sign.setText("已签收");
                        tv_status.setText("已签收");
                        btn_sign.setClickable(false);
                        btn_sign.setEnabled(false);
                        btn_sign.setFocusable(false);
                        btn_sign.setBackgroundColor(getResources().getColor(R.color.gray));

                    }else {
                        showToast(resultBaseBean.message);
                    }

                }catch (Exception e) {
                    showToast("签收失败");
                    e.printStackTrace();
                }

            }
        }, null);

    }
}